Trò chơi Tic-Tac-Toe, game đánh caro full source code
53.583 lượt xem;
- InputToEvent.cs
- UtilityScripts /
- Photon Unity Networking /
- Assets /
- project /
1 using UnityEngine;
2
3 /// <summary>
4 /// Utility component to forward mouse or touch input to clicked gameobjects.
5 /// Calls OnPress, OnClick and OnRelease methods on "first" game object.
6 /// </summary>
7 public class InputToEvent : MonoBehaviour
8 {
9
10 private GameObject lastGo;
11 public static Vector3 inputHitPos;
12 public bool DetectPointedAtGameObject;
13 public static GameObject goPointedAt { get; private set; }
14
15 private Vector2 pressedPosition = Vector2.zero;
16 private Vector2 currentPos = Vector2.zero;
17 public bool Dragging;
18
19 private Camera m_Camera;
20
21 public Vector2 DragVector
22 {
23 get { return this.Dragging ? this.currentPos - pressedPosition : Vector2.zero; }
24 }
25
26 void Start()
27 {
28 m_Camera = GetComponent<Camera>();
29 }
30
31 // Update is called once per frame
32 void Update()
33 {
34 if( DetectPointedAtGameObject )
35 {
36 goPointedAt = RaycastObject( Input.mousePosition );
37 }
38
39 if( Input.touchCount > 0 )
40 {
41 Touch touch = Input.GetTouch( 0 );
42 this.currentPos = touch.position;
43
44 if( touch.phase == TouchPhase.Began )
45 {
46 Press( touch.position );
47 }
48 else if( touch.phase == TouchPhase.Ended )
49 {
50 Release( touch.position );
51 }
52
53 return;
54 }
55
56 currentPos = Input.mousePosition;
57 if( Input.GetMouseButtonDown( 0 ) )
58 {
59 Press( Input.mousePosition );
60 }
61 if( Input.GetMouseButtonUp( 0 ) )
62 {
63 Release( Input.mousePosition );
64 }
65
66 if( Input.GetMouseButtonDown( 1 ) )
67 {
68 pressedPosition = Input.mousePosition;
69 lastGo = RaycastObject( pressedPosition );
70 if( lastGo != null )
71 {
72 lastGo.SendMessage( "OnPressRight", SendMessageOptions.DontRequireReceiver );
73 }
74 }
75 }
76
77
78 private void Press( Vector2 screenPos )
79 {
80 pressedPosition = screenPos;
81 this.Dragging = true;
82
83 lastGo = RaycastObject( screenPos );
84 if( lastGo != null )
85 {
86 lastGo.SendMessage( "OnPress", SendMessageOptions.DontRequireReceiver );
87 }
88 }
89
90 private void Release( Vector2 screenPos )
91 {
92 if( lastGo != null )
93 {
94 GameObject currentGo = RaycastObject( screenPos );
95 if( currentGo == lastGo ) lastGo.SendMessage( "OnClick", SendMessageOptions.DontRequireReceiver );
96 lastGo.SendMessage( "OnRelease", SendMessageOptions.DontRequireReceiver );
97 lastGo = null;
98 }
99
100 pressedPosition = Vector2.zero;
101 this.Dragging = false;
102 }
103
104 private GameObject RaycastObject( Vector2 screenPos )
105 {
106 RaycastHit info;
107 if( Physics.Raycast( m_Camera.ScreenPointToRay( screenPos ), out info, 200 ) )
108 {
109 inputHitPos = info.point;
110 return info.collider.gameObject;
111 }
112
113 return null;
114 }
115 }
2
3 /// <summary>
4 /// Utility component to forward mouse or touch input to clicked gameobjects.
5 /// Calls OnPress, OnClick and OnRelease methods on "first" game object.
6 /// </summary>
7 public class InputToEvent : MonoBehaviour
8 {
9
10 private GameObject lastGo;
11 public static Vector3 inputHitPos;
12 public bool DetectPointedAtGameObject;
13 public static GameObject goPointedAt { get; private set; }
14
15 private Vector2 pressedPosition = Vector2.zero;
16 private Vector2 currentPos = Vector2.zero;
17 public bool Dragging;
18
19 private Camera m_Camera;
20
21 public Vector2 DragVector
22 {
23 get { return this.Dragging ? this.currentPos - pressedPosition : Vector2.zero; }
24 }
25
26 void Start()
27 {
28 m_Camera = GetComponent<Camera>();
29 }
30
31 // Update is called once per frame
32 void Update()
33 {
34 if( DetectPointedAtGameObject )
35 {
36 goPointedAt = RaycastObject( Input.mousePosition );
37 }
38
39 if( Input.touchCount > 0 )
40 {
41 Touch touch = Input.GetTouch( 0 );
42 this.currentPos = touch.position;
43
44 if( touch.phase == TouchPhase.Began )
45 {
46 Press( touch.position );
47 }
48 else if( touch.phase == TouchPhase.Ended )
49 {
50 Release( touch.position );
51 }
52
53 return;
54 }
55
56 currentPos = Input.mousePosition;
57 if( Input.GetMouseButtonDown( 0 ) )
58 {
59 Press( Input.mousePosition );
60 }
61 if( Input.GetMouseButtonUp( 0 ) )
62 {
63 Release( Input.mousePosition );
64 }
65
66 if( Input.GetMouseButtonDown( 1 ) )
67 {
68 pressedPosition = Input.mousePosition;
69 lastGo = RaycastObject( pressedPosition );
70 if( lastGo != null )
71 {
72 lastGo.SendMessage( "OnPressRight", SendMessageOptions.DontRequireReceiver );
73 }
74 }
75 }
76
77
78 private void Press( Vector2 screenPos )
79 {
80 pressedPosition = screenPos;
81 this.Dragging = true;
82
83 lastGo = RaycastObject( screenPos );
84 if( lastGo != null )
85 {
86 lastGo.SendMessage( "OnPress", SendMessageOptions.DontRequireReceiver );
87 }
88 }
89
90 private void Release( Vector2 screenPos )
91 {
92 if( lastGo != null )
93 {
94 GameObject currentGo = RaycastObject( screenPos );
95 if( currentGo == lastGo ) lastGo.SendMessage( "OnClick", SendMessageOptions.DontRequireReceiver );
96 lastGo.SendMessage( "OnRelease", SendMessageOptions.DontRequireReceiver );
97 lastGo = null;
98 }
99
100 pressedPosition = Vector2.zero;
101 this.Dragging = false;
102 }
103
104 private GameObject RaycastObject( Vector2 screenPos )
105 {
106 RaycastHit info;
107 if( Physics.Raycast( m_Camera.ScreenPointToRay( screenPos ), out info, 200 ) )
108 {
109 inputHitPos = info.point;
110 return info.collider.gameObject;
111 }
112
113 return null;
114 }
115 }
Utility component to forward mouse or touch input to clicked gameobjects.
Calls OnPress, OnClick and OnRelease methods on "first" game object.
Update is called once per frame